# coding: UTF-8
# Filename : chain_home_api.py
# Company : imedia
# Author : zqz
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl

from django.http import HttpResponse
import chain_home_data_model
import time
'''
http://api.imedia.com/app/part/list
'''
		
#details list
def chain_home_list(request):
    #record log((frist action))
    httputils.footprint(request)
    #param
    cus_id = httputils.post(request, 'cus_id')#is customer id,more
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    
    ratio = httputils.post(request, 'ratio')
    
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(cus_id): cus_id = 1 
    #Verification
    if validate(request):
        return validate(request)
    #models
    output = ''
    output += chain_home_data_model.imedia_start 
    output += get_body_str(cus_id, ratio)
    output += chain_home_data_model.imedia_end
    return response(output)

#获取中间数据体
def get_body_str(cus_id, ratio):
    #sql
    sql = '''
    select slogan_path
    from chain_config
    where cus_id=%s
    '''
    #获取起始行
    body_str = '<info>'
    param = [int(cus_id)]
    obj = mysql.get(sql, param)
    if obj == None : return ''
    rs = str(obj[0])
    strlist = rs.split(';')
    for value in strlist:
        body_str += '<slogan>' +image_path(value, ratio, False)+'</slogan>'
    body_str += '</info>'

    #0 shop 1 branch 2 active
    sql = '''
    SELECT id,item_type,item_id,store_in_app,created_at FROM chain_home_item
    where config_id=%s
    '''
    param = [int(cus_id)]
    rsts = mysql.doSearch(sql, param)
    body_str_product = ''
    body_str_part = ''
    body_str_activity = ''
    #0 is 商品 1 is 分店 2 is 活动
    for obj in rsts:
        if (obj[1] == 0):
            body_str_product += body_shop(obj[2], obj[3], ratio, True)
        if (obj[1] == 1):
            body_str_part += body_part(obj[2], obj[3], ratio, True)
        if (obj[1] == 2):
            body_str_activity += body_activity(obj[2], obj[3], ratio, True)
    if (body_str_product <> ''): 
        body_str += '''<products>'''+body_str_product+'''</products>'''
    if (body_str_part <> ''): 
        body_str += '<parts>'+body_str_part+'</parts>'
    if (body_str_activity <> ''):
        body_str += '<activities>'+body_str_activity+'</activities>'
    return body_str
#fen dian
def body_part(item_id, store_in_app, ratio, is_thumb):
    sql = '''
    select id,name, address, images, tel, longitude, latitude, description
    from chain_part
    where id = %s
    '''
    obj = mysql.get(sql, [int(item_id)])
    body_template = chain_home_data_model.imedia_body_part
    body_template = body_template.replace('{id}', str(obj[0]))
    body_template = body_template.replace('{name}', str(obj[1]))
    body_template = body_template.replace('{address}', str(obj[2]))
    body_template = body_template.replace('{image}', fuck_liuyang(obj[3], ratio, is_thumb))
    body_template = body_template.replace('{tel}',str(obj[4]))
    body_template = body_template.replace('{longitude}',str(obj[5]))
    body_template = body_template.replace('{latitude}',str(obj[6]))
    body_template = body_template.replace('{storeinapp}',str(store_in_app))
    body_template = body_template.replace('{description}',utils.get_detail_text(str(obj[7])))
    return body_template



# 0 is chain_part=> shangping
def body_shop(item_id, store_in_app, ratio, is_thumb):
    sql = '''
    select name ,id, images, store_in_app, description from chain_product
    where id = %s
    '''
    obj = mysql.get(sql, [int(item_id)])
    body_template = chain_home_data_model.imedia_body_product
    body_template = body_template.replace('{id}', str(obj[1]))
    body_template = body_template.replace('{name}', str(obj[0]))
    body_template = body_template.replace('{category}', '商品')
    body_template = body_template.replace('{image}', fuck_liuyang(obj[2], ratio, is_thumb))
    body_template = body_template.replace('{storeinapp}',str(store_in_app))
    body_template = body_template.replace('{description}', utils.get_detail_text(str(obj[4])))
    return body_template


# 0 is chain_part=> shangping
def body_activity(item_id, store_in_app, ratio, is_thumb):
    sql = '''
    select id, name, start_time ,end_time  ,images, store_in_app, description from chain_activity
    where id = %s
    '''
    obj = mysql.get(sql, [int(item_id)])
    body_template = chain_home_data_model.imedia_body_activity
    body_template = body_template.replace('{id}', str(obj[0]))
    body_template = body_template.replace('{name}', str(obj[1]))
    body_template = body_template.replace('{category}', '活动')
    ISOTIMEFORMAT='%Y-%m-%d %X'
    start_time = time.strftime(ISOTIMEFORMAT,time.localtime(obj[2]))
    body_template = body_template.replace('{from}', start_time)
    end_time = time.strftime(ISOTIMEFORMAT,time.localtime(obj[3]))
    body_template = body_template.replace('{to}', end_time)
    body_template = body_template.replace('{image}', fuck_liuyang(obj[4], ratio, is_thumb))
    body_template = body_template.replace('{storeinapp}',str(store_in_app))
    body_template = body_template.replace('{description}',utils.get_detail_text(str(obj[6])))
    return body_template








